package com.ey.controller;

import com.ey.config.ConstantProperties;
import com.ey.util.AliyunOOSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

@Controller
@RequestMapping("/oss")
//@Slf4j
public class FileController {
    @Autowired
    private AliyunOOSUtil aliyunOOSUtil;
    @Autowired
    private ConstantProperties constantProperties;

    @GetMapping("/toUploadBlog")
    public String toUploadBlog() {
        return "oss/upload";
    }

    @PostMapping("/toUploadBlog")
    public String toUploadBlogPost(MultipartFile file) {
        System.out.println("=========>文件上传");
//        log.info("=========>文件上传");
        try {

            if (null != file) {
                String filename = file.getOriginalFilename();
                if (!"".equals(filename.trim())) {
                    File newFile = new File(filename);
                    FileOutputStream os = new FileOutputStream(newFile);
                    os.write(file.getBytes());
                    os.close();
                    file.transferTo(newFile);
                    //上传到OSS
                    String uploadUrl = aliyunOOSUtil.upload(newFile);
                }

            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "oss/index";
    }

    @GetMapping("/getObjectList")
    @ResponseBody
    public List<String> getObjectList() {
        String bucketName = constantProperties.getBucketname();
        System.out.println(bucketName);
        List<String> objectList = aliyunOOSUtil.getObjectList(bucketName);
        return objectList;
    }
}